package com.zhazha.earlybird.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zhazha.earlybird.beans.CityInfo;
import com.zhazha.earlybird.beans.ProvinceInfo;

public class CityDAO {

	private SQLiteDatabase mDatabase;
	private static final String CITY_TABLE = "cities";
	
	private static final String CITY_ID 				= "id";
	private static final String CITY_NAME				= "name";
	private static final String CITY_PROVINCE_ID		= "province_id";
	private static final String CITY_SORT				= "sort";
	
	
	public CityDAO(SQLiteDatabase db) {
		mDatabase = db;
	}
	
	
	public boolean insert(CityInfo city) {
		
		try {
			ContentValues cv = new ContentValues();
			cv.put(CITY_ID, 	city.getId());
			cv.put(CITY_NAME,   city.getName());
			cv.put(CITY_PROVINCE_ID, city.getProvinceId());
			
			this.mDatabase.insert(CITY_TABLE, null, cv);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	public boolean delete(int id) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.delete(CITY_TABLE, whereClause, whereArgs);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(int id, ContentValues cv) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.update(CITY_TABLE, cv, whereClause, whereArgs);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public int queryIdByName(String name) {
		try {
			String selection = "name=?";
			String[] selectionArgs = {name};
			Cursor c = this.mDatabase.query(CITY_TABLE, null, selection, selectionArgs, null, null, null);
			
			if(c.moveToFirst()) {
				return c.getInt(c.getColumnIndex(CITY_ID));
			}
			
			return 0;
			
		}catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public String queryById(int id) {
		String city = null;
		
		try {
			String selection = "id=?";
			String[] selectionArgs = {String.valueOf(id)};
			Cursor c = this.mDatabase.query(CITY_TABLE, null, selection, selectionArgs, null, null, null);

			if(c.moveToFirst()) {
				city = c.getString(c.getColumnIndex(CITY_NAME));
			}
			
			return city;
			
		}catch (Exception e) {
			e.printStackTrace();
			return city;
		}
	}
	
	public CityInfo[] queryAll() {
		CityInfo[] cities = null;
		
		try {
			
			 Cursor c = this.mDatabase.query(CITY_TABLE, null, null, null, null, null, null);
			
			if(c.moveToFirst()) {
				cities = new CityInfo[c.getCount()];
				for (int i = 0; i < c.getCount(); i++) {
					
					cities[i] = new CityInfo();
					cities[i].setId(c.getInt(c.getColumnIndex(CITY_ID)));
					cities[i].setName(c.getString(c.getColumnIndex(CITY_NAME)));
					cities[i].setProvinceId(c.getInt(c.getColumnIndex(CITY_PROVINCE_ID)));
					cities[i].setSort(c.getInt(c.getColumnIndex(CITY_SORT)));

					c.moveToNext();
				}
				
			}
			
			return cities;
			
		}catch (Exception e) {
			e.printStackTrace();
			return cities;
		}
	}

}
